<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回测系统 - 低位龙头股分析系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        .card-hover:hover {
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            transition: box-shadow 0.3s ease-in-out;
        }

        .report-card {
            height: 100%;
        }

        .positive-return {
            color: #28a745;
        }

        .negative-return {
            color: #dc3545;
        }
    </style>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">低位龙头股分析系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/backtesting">回测系统</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/backtesting/settings">回测设置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/news">市场新闻</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row mb-4">
            <div class="col">
                <h2><i class="bi bi-graph-up"></i> 回测系统</h2>
                <p class="lead">验证低位龙头股分析策略的有效性</p>
            </div>
            <div class="col-auto">
                <a href="/backtesting/settings" class="btn btn-primary">
                    <i class="bi bi-gear"></i> 运行新回测
                </a>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">回测报告列表</h5>
                    </div>
                    <div class="card-body">
                        {% if reports %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>报告名称</th>
                                        <th>生成时间</th>
                                        <th>测试数量</th>
                                        <th>日期范围</th>
                                        <th>平均收益率</th>
                                        <th>超额收益</th>
                                        <th>胜率</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for report in reports %}
                                    <tr>
                                        <td>
                                            <a href="/backtesting/detail/{{ report.file_name }}">
                                                {{ report.file_name }}
                                            </a>
                                        </td>
                                        <td>{{ report.timestamp }}</td>
                                        <td>{{ report.total_tests }}</td>
                                        <td>{{ report.date_range }}</td>
                                        <td
                                            class="{% if report.overall_avg_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(report.overall_avg_return) }}%
                                        </td>
                                        <td
                                            class="{% if report.overall_excess_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(report.overall_excess_return) }}%
                                        </td>
                                        <td>{{ "%.2f"|format(report.overall_win_rate) }}%</td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="/backtesting/detail/{{ report.file_name }}"
                                                    class="btn btn-outline-primary">
                                                    <i class="bi bi-eye"></i>
                                                </a>
                                                <button class="btn btn-outline-danger delete-report"
                                                    data-report="{{ report.file_name }}">
                                                    <i class="bi bi-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="alert alert-info">
                            <i class="bi bi-info-circle"></i> 暂无回测报告，请先运行回测
                        </div>
                        <div class="text-center mt-3">
                            <a href="/backtesting/settings" class="btn btn-primary">
                                <i class="bi bi-gear"></i> 运行新回测
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">回测系统说明</h5>
                    </div>
                    <div class="card-body">
                        <p>回测系统用于验证低位龙头股分析策略的有效性，通过历史数据模拟策略的表现。</p>
                        <h6>主要功能：</h6>
                        <ul>
                            <li>对历史分析结果进行回测，计算不同时间段的收益率</li>
                            <li>与市场基准（上证指数）进行对比，计算超额收益</li>
                            <li>计算策略胜率、最大收益、最小收益等指标</li>
                            <li>生成可视化图表，直观展示策略表现</li>
                        </ul>
                        <h6>使用方法：</h6>
                        <ol>
                            <li>点击"运行新回测"按钮，进入回测设置页面</li>
                            <li>选择要回测的分析结果目录，设置回测参数</li>
                            <li>点击"开始回测"按钮，等待回测完成</li>
                            <li>查看回测报告，分析策略表现</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 删除确认模态框 -->
    <div class="modal fade" id="deleteModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">确认删除</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>确定要删除报告 <span id="reportName"></span> 吗？此操作不可恢复。</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" id="confirmDelete">确认删除</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function () {
            // 删除报告
            let reportToDelete = '';

            $('.delete-report').click(function () {
                reportToDelete = $(this).data('report');
                $('#reportName').text(reportToDelete);
                $('#deleteModal').modal('show');
            });

            $('#confirmDelete').click(function () {
                $.ajax({
                    url: '/backtesting/delete/' + reportToDelete,
                    type: 'POST',
                    success: function (response) {
                        if (response.success) {
                            $('#deleteModal').modal('hide');
                            location.reload();
                        } else {
                            alert('删除失败: ' + response.message);
                        }
                    },
                    error: function () {
                        alert('删除请求失败，请重试');
                    }
                });
            });
        });
    </script>
</body>

</html>